package com.example.leetcode;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 股票问题 2
 * 单调栈
 */
public class Demo0122 {
    public static void main(String[] args) {
        Deque<Integer> stack=new ArrayDeque<>();
    }
    public int maxProfit(int[] prices) {
        int len =prices.length;
        Deque<Integer> stack=new ArrayDeque<>(len);
        int ans=0;
        for (int i = 0; i < len; i++) {
            //**是否考虑出栈**
            if (!stack.isEmpty() && stack.peekLast()> prices[i]){
                ans+=stack.peekLast()-stack.peekFirst();
                stack.clear();
            }
            stack.addLast(prices[i]);
        }
        return ans+stack.peekLast()-stack.peekFirst();
    }
}
